<%-- 
    Document   : menageUsers
    Created on : 24-apr-2014, 10.42.17
    Author     : Mark
--%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.Format"%>
<%@page import="it.evento.EventPrintable"%>
<%@page import="it.evento.Events"%>
<%@page import="java.util.List"%>
<%@page import="it.evento.controllers.Controller"%>
<%@page import="it.evento.utils.genpage.Article"%>
<%@page import="it.evento.utils.genpage.ArticleRow"%>
<%@page import="it.evento.utils.genpage.RowCell"%>
<%@page import="it.evento.Users"%>
<%@page import="it.evento.utils.genpage.BlogPost"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    boolean isLoggedUser = false;
    Users loggedUser = null;
    
    if(session.getAttribute("loggedUser")!=null) {
        loggedUser = (Users)session.getAttribute("loggedUser");
        isLoggedUser = true;
    }
    else {
        isLoggedUser = false;
    }
%>    

<!DOCTYPE html>
<html>
    <head>
        <title>EvenTO</title>

        <jsp:include page="html/metadata.jsp"/>
        
         <script>
            $(function() {
              $('#cancelUsers').click(function(e) {
                   console.log("[Cancel Users]");
                   var delArray = [];                 
                   $('input[type=checkbox]').each(function () {
                       console.log("chckbox id: "+this.id+" checked? "+this.checked);
                       if(this.checked)
                           delArray.push(this.id);
                   });
                   console.log("delArray: "+delArray.toString());
                   
                   if(delArray.length>0){
                    $.post("Controller",
                                 {type: "cancelUsers",
                                     idUsers: delArray.toString()
                                 }, 
                             function(data) {                            
                                if (data.status === 0) {
                                    $('#msgOut').html("<ul>" + data.message+"</ul>");
                                    location.reload();
                                }
                                else {
                                    $('#msgOut').html("<ul>" + data.status + ": " + data.message+"</ul>");
                                }
                            },
                                 'json');
                   }else{
                       $('#msgOut').html("<ul><li>Selezionare un utente da rimuovere</li></ul>");
                   }
                   console.log("[/Cancel Users]");
                });  
                
                var modArray=[];
                $('#modUsers').click(function(e) {
                   console.log("[Mod Users]");        
                   modArray= []; //ripulisce array
                   $('#msgOut').html(" ");
                   $('input[type=checkbox]').each(function () {
                       console.log("chckbox id: "+this.id+" checked? "+this.checked);
                       if(this.checked)                          
                           modArray.push(this.id);
                               
                   });
                   //console.log("modArray "+modArray.toString());
                   if(modArray.length>0){
                        $.post("Controller",
                                     {type: "forwardUsers",
                                         idUsers: modArray.toString()
                                     },
                             function(data) {
                                 if (data.status === 0) {                                     
                                    $('#htmlModify').html(data.message);                                        
                                 }else {
                                    $('#msgOut').html("<ul>" + data.status + ": " + data.message+"</ul>");
                                 }
                             },
                                     'json');
                    }else{
                       $('#msgOut').html("<ul><li>Selezionare un utente da modificare</li></ul>");
                   }
                   console.log("[/Mod Events]");
                });
                
                //Bottoni di modifica sul singolo evento selezionato
                $("#htmlModify").on('click', '.evento-button', function() {
                    var recupero_id = $(this).attr("id");
                    var split=recupero_id.split('_');
                    console.log("id: "+split[0]);
                    console.log("type: "+split[1]);
                    modArray.splice(modArray.indexOf(split[0]), 1);//cancella elemento dalla lista
                    if(split[1]==="add"){                        
                        $.post("Controller",
                                        {type: "modifyUsers",
                                            idModUsers:split[0],
                                            usernameModUser: $('#'+split[0]+'_username').val(),
                                            nameModUser: $('#'+split[0]+'_name').val(),
                                            surnameModUser: $('#'+split[0]+'_surname').val(),
                                            birthModUser: $('#'+split[0]+'_birth').val(),
                                            companyModUser: $('#'+split[0]+'_company').val(),
                                            typeModUser: $('#'+split[0]+'_type').val(),
                                            upgradeModUser: $('#'+split[0]+'_upgrade').val(),                                           
                                            idUsers: modArray.toString()
                                        },
                                function(data) {
                                    if (data.status === 0) {                                         
                                       $('#htmlModify').html(data.message);     
                                    }else {
                                       $('#msgOut').html("<ul>"+ data.message+"</ul>");
                                       $('#htmlModify').html(""); 
                                    }
                                },
                                        'json');
                   }else{//===cancel ricarica la pagina senza quel form di modifica
                       $.post("Controller",
                                     {type: "forwardUsers",
                                         idUsers: modArray.toString()
                                     },
                             function(data) {
                                 if (data.status === 0) {                                     
                                    $('#htmlModify').html(data.message);                                        
                                 }else {
                                    $('#htmlModify').html("");   
                                 }
                             },
                                     'json');
                   }
                  
                 });
            });
         </script>
    </head>
    <body>
        <div id="evento-main">
            <!-- header -->
            <jsp:include page="html/header.jsp" />
            <!-- /header -->

            <!-- top-menu -->
            <%@include file="html/top-menu.jsp" %>
            <!-- /top-menu -->

            <div class="evento-sheet clearfix">
                <div class="evento-layout-wrapper">
                    <div class="evento-content-layout">
                        <div class="evento-content-layout-row">
                            <div class="evento-layout-cell evento-content">
                                <% if(isLoggedUser) { %>
                                    <%
                                         List<Users> usersList=(List<Users>) request.getAttribute("userList");
                                         SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
                                         
                                         String s="<table border='1'>";
                                         s += "<tr>"
                                                     + "<td> </td>"
                                                     + "<td>Username</td>"
                                                     + "<td>Nome</td>"
                                                     + "<td>Cognome</td>"
                                                     + "<td>Compleanno</td>"
                                                     + "<td>Organizzazione</td>"
                                                     + "<td>Tipo</td>"
                                                     + "<td>Upgrade</td>"
                                                     + "</tr>";                                         
                                         
                                         for (Users u : usersList) {
                                                 s += "<tr>";
                                                 s += "<td><input type='checkbox'  id='" + u.getId() + "'/></td>";
                                                 s += "<td>" + u.getUsername() + "</td>";
                                                 s += "<td>" + u.getName() + "</td>";
                                                 s += "<td>" + u.getSurname() + "</td>";
                                                 try {
                                                     s += "<td>" + DATE_FORMAT.format(u.getBirth()) + "</td>";
                                                 } catch (java.lang.NullPointerException e) {//no data compleanno                                                 
                                                     s += "<td></td>";
                                                 }
                                                 s += "<td>" + u.getCompanyname() + "</td>";
                                                 if (u.getType() == 1) {
                                                     s += "<td>Admin</td>";
                                                 }
                                                 if (u.getType() == 2) {
                                                     s += "<td>Publisher</td>";
                                                 }
                                                 if (u.getType() == 3) {
                                                     s += "<td>Registred</td>";
                                                 }
                                                 s += "<td>" + u.getUpgrade() + "</td>";
                                                 s += "</tr>";
                                             }
                                         s+="</table><br>";                                         
                                         s+="<input type='button' value='Modifica' id='modUsers' class='evento-button' />";
                                         s+="<input type='button' value='Elimina' id='cancelUsers' class='evento-button' />";
                                         s+="<a href='createAccount.jsp'><input type='button' value='Registra' id='createUser' class='evento-button' /></a>";
                                         s+="<div id='msgOut'></div>";
                                                 
                                    %>     
                                    <%=
                                           new BlogPost()
                                                   .setTitle("Your Users")
                                                   .setContent(s)
                                                   .print()
                                     %>  
                                     <div id="htmlModify"></div>
                                <% }else{ %>                     
                                    <h3>Non hai i diritti necessari per accedere a questa pagina</h3>
                                <%}%>
          
                            </div>

                            <!-- sidebar -->
                            <jsp:include page="html/sidebar.jsp" />
                            <!-- /sidebar -->
                            
                        </div>
                    </div>
                </div>

                <!-- footer -->
                <%@include file="html/footer.jsp" %>
                <!-- /footer -->

            </div>
        </div>
    </body>
</html>
